package com.example.demo.demos.web;

import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;

import java.util.concurrent.*;
@EnableAsync
@Configuration
public class ThreadConfig {
        private ThreadPoolExecutor executor;
        public ThreadPoolExecutor getExecutor(){
            if (executor == null || executor.isShutdown()){
                executor = new ThreadPoolExecutor(
                        5,  // 核心线程数
                        10,  // 最大线程数
                        1,  // 当线程数大于核心线程数时，多余的空闲线程存活的最长时间
                        TimeUnit.SECONDS,  // 存活时间单位
                        new ArrayBlockingQueue<>(100),  // 任务队列，用来储存等待执行任务的队列
                        Executors.defaultThreadFactory(),
                        new ThreadPoolExecutor.AbortPolicy()
                );
            }
            return executor;
        }
    }
